<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginFrom"
        :rules="loginFromRules"
        label-width="40px"
        class="login_form"
      >
        <el-form-item label="账" prop="username">
          <el-input
            v-model="loginFrom.username"
            :clearable="true"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密" prop="password">
          <el-input
            v-model="loginFrom.password"
            type="password"
            :clearable="true"
          >
            <i slot="prefix" class="iconfont icon-mima"></i>
          </el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" plain @click="login">登录</el-button>
          <el-button type="info" plain @click="resetLoginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 这是登录表单的数据绑定对象
      loginFrom: {
        username: "admin",
        password: "123456",
      },
      //这是表单的验证规则对象
      loginFromRules: {
        username: [
          //required 意思是必填项
          { required: true, message: "请输入正常的用户名", trigger: "blur" },
          {
            min: 5,
            max: 10,
            message: "用户名长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 10,
            message: "密码长度在 6 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //点击重置按钮,重置登录表单
    resetLoginFrom() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        //请求方法 请求地址
        //解构复制出一个data属性 重命名为res
        const { data: res } = await this.$http.post("login", this.loginFrom);
        if (res.meta.status !== 200) return this.$message.error("登录失败");
        this.$message.success("登录成功");
        // console.log(res)
        // 1.将登录之后的token保存到客户端sessionStorage中
        //1.1项目中出了登录之外的其他API接口,必须在登录之后才能访问
        //1.2token只应在当前网站打开期间生效,所以将token保存在sessionStorage中
        window.sessionStorage.setItem("token", res.data.token);
        //2.通过编程式导航跳转到后台主页,路由地址是/home
        this.$router.push("/home");
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.login_container {
  background-color: #eee;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;

  .login_box {
    width: 450px;
    height: 350px;
    background-color: #fff;
    border-radius: 3px;
    position: relative;
    .avatar_box {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px salmon;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: silver;
      img {
        width: 100%;
        //   height: 100%;
        border-radius: 50%;
        background-color: salmon;
      }
    }
  }
}
.login_form {
  position: absolute;
  bottom: 30px;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>

